package com.bafa.objects.season;

import com.bafa.objects.LeagueRules;
import com.bafa.objects.Scorer;

import com.bafa.objects.Match;
import com.bafa.objects.player.Player;
import com.bafa.objects.team.Team;

import com.bafa.services.RetrieveServices;
import com.bafa.util.comparators.LeagueComparator;
import com.bafa.util.comparators.MatchByDateComparator;

import com.bafa.util.comparators.TopScorerComparator;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.TreeSet;

public class Division {
    private boolean completed;
    
    private int division;
    private LeagueRules leagueRules;
    private List matches = new ArrayList();
    private TreeMap scorers = new TreeMap();
    private int seasonId;
    private TreeSet teams = new TreeSet();
    public Division() {
    }

	public void addTeams(int team){
		teams.add(team);
	}
	public void addMatches(int match){
		matches.add(match);
	}
	public void addScorers(String name,String otherName){
		scorers.put(name,otherName);
	}
	
    public void enterMatch(Match match){
       matches.add(match.getID());
        
    }
    public void enterScorer(String name){
		TopScorerComparator scorerComparator = new TopScorerComparator();
		scorerComparator.setSeasonDivision(seasonId,division);
		if(scorers==null)
			scorers = new TreeMap(scorerComparator);
        if(!scorers.containsKey(name)){
            scorers.put(name,name);
        }
    }

    public void enterTeam(int teamId){
		LeagueComparator leagueComparator = new LeagueComparator();
		leagueComparator.setSeasonId(seasonId);
		if(teams==null){
			teams = new TreeSet(leagueComparator);	
		}
	    teams.add(teamId);
    }

    public int getDivision() {
        return division;
    }

    public LeagueRules getLeagueRules() {
        return leagueRules;
    }

    public List getMatches() {
        return matches;
    }

    public TreeMap getScorers() {
        return scorers;
    }

    public int getSeasonId() {
        return seasonId;
    }

    public TreeSet getTeams() {
        return teams;
    }

    public boolean isCompleted() {
        return completed;
    }

    public Integer[] matchArray(){
		return (Integer[])matches.toArray(new Integer[matches.size()]);
	}

    
    
    
    public void refreshTeams(){
		TreeSet origTeams = teams;
		LeagueComparator leagueComparator = new LeagueComparator();
		leagueComparator.setSeasonId(seasonId);
		if(teams==null){
			teams = new TreeSet(leagueComparator);	
		}
		teams.addAll(origTeams);
	}
    
    public void removeFixture(Match match){
		matches.remove(new Integer(match.getID()));
	}
	public void setCompleted(boolean completed) {
        this.completed = completed;
    }
	
	public void setDivision(int division) {
        this.division = division;
    }
	
	public void setLeagueRules(LeagueRules leagueRules) {
        this.leagueRules = leagueRules;
    }
	
	public void setMatches(List matches) {
        this.matches = matches;
    }
	
	public void setScorers(TreeMap scorers) {
        this.scorers = scorers;
    }
	
	public void setSeasonId(int seasonId) {
        this.seasonId = seasonId;
    }
	
	public void setTeams(TreeSet teams) {
        this.teams = teams;
    }
	
	public Team[] teamArray(){
		Team[] teams = new Team[this.teams.size()];
		Integer[] teamArray = teamIdArray();
		for(int i=0;i<this.teams.size();i++){
			int teamId = teamArray[i];
			teams[i] = RetrieveServices.getTeam(""+teamId);
		}
		
		return teams;
	}
	
	public Integer[] teamIdArray(){
		Integer[] teamArray = (Integer[])(teams.toArray(new Integer[teams.size()]));
		return teamArray;
	}
	
	public String[] topScorers(){
		return (String[])getScorers().values().toArray(new String[scorers.size()]);
	}
	
}
